var gold_top = "";
var gold_bottom = "";
var intervalId = "";
var arraySize = "";
var changeSec = "6000";
$().ready(function(){
	//random show maingold
	arraySize = 4;
	var maingoldNum = $.random(arraySize);
	$("div.bigbn-menu > a").eq(maingoldNum-1).addClass("bigbn-f");
	$("#main-gold-" + maingoldNum).show();
	init4Imgs();
	//每六秒自動跳imgs,当滑鼠移到上面时则不执行
	$("div.bigbn-menu > a").each(function(index){
		$(this).mouseenter(function(){
			$("div.bigbn-menu > a").each(function(index,item){
				$(this).removeClass("bigbn-f");
			});
			$("div[id*='main-gold-']").each(function(){
				$(this).hide();
			});
			$("div.bigbn-menu > a").eq(index).addClass("bigbn-f");
//			$("#main-gold-" + (index+1)).show();
			var productImageBlock = $("#main-gold-" + (index+1));
			productImageBlock.show();
			var productImages = productImageBlock.find("img");
			productImages.each(function(){
				if ($(this).attr('src') != $(this).attr('original')) {
					$(this).attr('src', $(this).attr('original'));
				}
			});
			gold_top = $(this);
			clearInterval(intervalId);
		});
		$(this).mouseleave(function(){
			gold_top = "";
			intervalId = setInterval(showRotatingImg,changeSec);
		});
	});
	$("div[id*='main-gold-']").each(function(){
		$(this).mouseenter(function(){
			gold_bottom = $(this);
			clearInterval(intervalId);
		});
		$(this).mouseleave(function(){
			gold_bottom = "";
			intervalId = setInterval(showRotatingImg,changeSec);
		});
	});
	//hotselect
	var hotselSize = $("div.hotsel-box").length;
	var selNum = $.random(hotselSize);
	$("div.hotselect > div.hotsel-box").each(function(){
		$(this).hide();
	});
	$("div.hotselect > div.hotsel-box").eq(selNum).show();	
});
function showRotatingImg(){
	//共有幾個tab
	var myImg = new Array(arraySize);
	for( i = 0 ; i < arraySize ; i++ ){
		myImg[i] = i;
	}
	//要取幾張來播放
	var playImg = new Array(arraySize);
	//目前播放到第幾張
	var nowIndex = 0;
	var tempNowIndex = 0;
	$("div.bigbn-menu > a").each(function(index,item){
		var thisClass = $(this).attr("class");
		if ( "bigbn-f" == thisClass ){
			nowIndex = index;
			$(this).removeClass("bigbn-f");
		}
	});
	tempNowIndex = nowIndex;
	$("div[id*='main-gold-']").each(function(){
		$(this).hide();
	});
	tempNowIndex++;
	if (tempNowIndex > (arraySize-1)) tempNowIndex = 0;
	//eq從0開始算
	$("div.bigbn-menu > a").eq(myImg[tempNowIndex]).addClass("bigbn-f");
	$("#main-gold-" + (parseInt(myImg[tempNowIndex],10)+1)).show();
}	
function init4Imgs(){
	if ( gold_bottom == "" && gold_top == "" )
		intervalId = setInterval(showRotatingImg,changeSec);
}